<%@page import="edu.ipccenter.toster.*"%>
<%@page import="java.math.BigInteger"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="additional.*"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<%!    String suite = "";
    SuiteUtils suites = null;
    String suiteToEdit = null;
    PrintInfofromBean print = null;
    Suite edsuite = null;
    InitialContext ic = null;
    TestCaseUtils tcutils = null;
    TestUtils tu = null;
%>
<%
    ic = new InitialContext();
    tcutils = (TestCaseUtils) ic.lookup("java:global/Tost/Tost-ejb/TestCaseUtils");
    suites = (SuiteUtils) ic.lookup("java:global/Tost/Tost-ejb/SuiteUtils");
    tu = (TestUtils) ic.lookup("java:global/Tost/Tost-ejb/TestUtils");
%>
<%
    if (request.getParameter("addNewQuest") != null) {
        String questname = request.getParameter("questname");
        TestCase tc = tcutils.create(questname,
                request.getParameter("questdesc"), request.getParameter("question"));
        edsuite.addTestCase(tc.getID());
        
        
        String theme = request.getParameter("theme");
        Collection<Name> col = tu.getThemes();
        Iterator<Name> iter = col.iterator();
        while (iter.hasNext()) {
            Name n = iter.next();
            if (n.getName().equals(theme)) {
                tc.addTheme(n.getID());     
            }   
        }
        short i = 1;
        
        while (request.getParameter("answer" + i) != null) {
            Answer ans = new Answer();
            ans.setAnsNum(i);
            ans.setContent(request.getParameter("answer" + i));
            if (request.getParameter("rightans") == ("ans" + i)) {
                ans.setCorrectness(true);
            } else ans.setCorrectness(false);
            ans.setID(tc.getID());
            tc.addAnswer(ans);
        }
    }
%>
<script type="text/javascript">
    var i = 1;
    function addRows(){
        var TABLE = document.getElementById('ansTable');
        var BODY = TABLE.getElementsByTagName('tbody')[0];
        var TR = document.createElement('tr');
        var TD = document.createElement('td');
        var CB = document.createElement('input');
        CB.setAttribute("type", "checkbox");
        //CB.setAttribute("name", "rightans");
        CB.name = "rightans";
        CB.setAttribute("value", "ans" + i);
        TD.appendChild(CB);
        TR.appendChild (TD);

        var TD = document.createElement('td');
        var INPUT = document.createElement('input');
        INPUT.setAttribute("name", "answer" + (i++));
        INPUT.setAttribute("type", "text");
        INPUT.setAttribute("class", "content");
        TD.appendChild(INPUT);
        TR.appendChild (TD);
        BODY.appendChild(TR);
        TABLE.appendChild(BODY);
    }
</script>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Creating tests</title> 
            <link rel="stylesheet" href="style/style.css" type="text/css" />
            <link rel="stylesheet" href="style/tabs.css" type="text/css" media="screen, projection"/>

            <script type="text/javascript" src="style/jquery-1.3.2.min.js"></script>
            <script type="text/javascript" src="style/jquery-ui-1.7.custom.min.js"></script>
            <script type="text/javascript">
                $(function() {
                    var $tabs = $('#tabs').tabs();
                <%
                        if (request.getParameter("curTab") != null) {
                            out.println("$tabs.tabs(\"select\", \"NewSuite\")");
                        }
                        if (request.getParameter("addQuestion") != null) {
                            out.println("$tabs.tabs(\"select\", \"NewSuite\")");
                        }
                        if (request.getParameter("addQuestion1") != null) {
                            out.println("$tabs.tabs(\"select\", \"EditSuite\")");
                        }
                        suiteToEdit = request.getParameter("suiteToEdit");
                        if (suiteToEdit != null) {
                            out.println("$tabs.tabs(\"select\", \"EditSuite\")");
                        }
                %>
                    });
            </script>    
    </head>
    <body>
        <img id="hat" src="style/brainImg.jpg" />
        <h1>Editing tests</h1>
        <!-- ------------------------------------------------------------------------ -->
        <div id="tabs">

            <ul>
                <li><a href="#Suites">Suites</a></li>
                <li><a href="#NewSuite">Create new suite</a></li>
                <li><a href="#EditSuite">Edit suite</a></li>
            </ul>


            <div id="Suites" class="ui-tabs-panel ui-tabs-hide">
                <%
                    //out.println(bean.getName());
                    Suite parent = suites.findByID(new BigInteger("1000"));
                %> 
                <ul>
                    <li> <%out.print(parent.getName());%> 
                        <%
                            print = new PrintInfofromBean(new PrintWriter(out), suites, tcutils);
                            print.buildSuiteTree(parent.getID());
                        %>
                    </li>
                </ul>

            </div>

            <div id="NewSuite" class="ui-tabs-panel">

                <form id="newTest" method="post" target="_self" onsubmit="return submitForm();">
                    Enter a new suit name:
                    <input type="text" name="newsuitename" />
                    <textarea name="newsuitedesc" rows="3" cols="100" 
                              placeholder="Enter a new suite description" style="width: 90%;"></textarea>
                    <%
                        out.println("<p> And choose a parent suite:<select name=\"newsuiteparent\" size=\"1\">");
                        String p = suites.findByID(new BigInteger("1000")).getName();
                        out.println("<option value=\"" + p + "\">" + p + "</option>");
                        print.getAllSuites(new BigInteger("1000"), "&nbsp;&nbsp;");

                        out.println("</select></p>");
                    %>                    
                    <input type="submit" id="newSuiteName" name="ok" value="OK" />
                </form> 
                <%
                    if (request.getParameter("ok") != null)
                        if (!request.getParameter("newsuitename").isEmpty()) {
                            suites.create(request.getParameter("newsuitename"), 
                                    request.getParameter("newsuitedesc"), 
                                    new BigInteger(request.getParameter("newsuiteparent")));
                        }
                        %>
            </div>


            <div id="EditSuite" class="ui-tabs-panel">
                <% 
                if (suiteToEdit != null) {
                    edsuite = suites.findByID(new BigInteger(suiteToEdit));
    
                    out.println("<h3>" + edsuite.getName() + "</h3>");
                %>
                <h4> Desciption </h4>
                <blockquote>
                    <%
                        out.println(edsuite.getDescription());
                    %>
                </blockquote>
                <h4> Parent Suite </h4>
                <blockquote>
                    <%
                        out.println(edsuite.getParent().getName());
                    %>
                </blockquote>
                <h4> List of Questions </h4>
                <!-- __________________________________________________________________________ -->
                <table border="1">
                    <tr>
                        <th>ID</th>
                        <th>Questions</th>
                        <th>Answers</th>
                        <th>Themes</th>
                        <th>Description</th>
                    </tr>
                    <%
                        print.getSuiteQuestions(edsuite);
                    %>

                </table>
                <form name="addquest" method="post">
                    <button type="submit" name="addQuestion1" value="Add">Add</button>
                </form>

                <%
                     if (request.getParameter("addQuestion1") != null) {
                %>

                <div class="block">       
                    Question:
                    <form name="newquest" method="get">
                        <input name="questname" type="text" placeholder="Enter a question name"/>
                        <textarea name="question" rows="3" cols="100" placeholder="Enter a new question" style="width: 90%"></textarea>
                        <p>Theme: <input type="text" name="theme" placeholder="enter a theme" /></p>
                        Description
                        <p>
                            <textarea name="questdesc" rows="3" cols="100" 
                                      placeholder="Enter a question description" style="width: 90%"></textarea></p>
                        Enter answers and point the right ones:
                        <table id="ansTable">
                            <tr>
                                <td class ="check"><input type="checkbox" name="rightans" value="ans1" /></td>
                                <td><input type="text"  name="answer1" class ="content" /> </td>
                            </tr>
                        </table>
                        <input type="button" onclick="addRows();" value="Add Answer" /> <br />
                        <input style="padding: 5px; margin: 0 50%;" type="submit" 
                               value="Add Question" name="addNewQuest"/>
                    </form>

                </div>

                <% }}%>

                <!--____________________________________________________________________________-->


            </div>
        </div>


    </body>
</html>
